package org.steven.crm.core.utils;

import cn.hutool.core.img.ImgUtil;
import org.apache.tika.Tika;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.List;

/**
 * @Author wuzw
 * @Date 2024/1/4 17:53
 * @Version 1.0
 */
public class ImageUtil {
    private static String[] imageFormats = {"jpeg", "jpg", "png", "gif", "logo", "webp", "image/jpeg", "image/jpg", "image/png", "image/gif", "image/logo", "image/webp"};
    private static List<String> imageFormatList = Arrays.asList(imageFormats);

    public static boolean isImageFile(String fileFormat) throws Exception {
        return imageFormatList.contains(fileFormat.toLowerCase());
    }

    public static long[] getImageWH(File file) throws Exception {
        BufferedImage bufferedImage = ImgUtil.read(file);
        long[] wh = new long[2];
        wh[0] = bufferedImage.getWidth();
        wh[1] = bufferedImage.getHeight();
        return wh;
    }

    public static long[] getImageWH(FileInputStream fis) throws Exception {
        BufferedImage bufferedImage = ImgUtil.read(fis);
        long[] wh = new long[2];
        wh[0] = bufferedImage.getWidth();
        wh[1] = bufferedImage.getHeight();

        return wh;
    }

    public static long getFileSize(File file) throws Exception {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            return fis.available();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }

}
